/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dvd.contoller;

import com.dvd.model.DVDItem;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 17296375-7
 */
public class AddDVDServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AddDVDServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AddDVDServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        List errorMsg = new ArrayList();
        
        try {
            //captura de datos desde el dformuulatrio add_dvd.html
            
            String title=request.getParameter("title");
            String year=request.getParameter("year");
            String genre=request.getParameter("newGenre");
            
            if (( genre==null)||(genre.trim().length()==0)){
                
                genre=request.getParameter("genre");
            }
            
            
            //validacion
            
            if (( title==null)||(title.trim().length()==0)){
                
                errorMsg.add("Debe ingresar un nombre");
            }
            
            if (( year==null)||(year.trim().length()==0)){
                
                errorMsg.add("Debe ingresar un año");
            } else if (!year.trim().matches("\\d\\d\\d\\d")){
                   errorMsg.add("Debe ingresar un año en numeros"); 
            
            }
            
            if (!errorMsg.isEmpty()){
            request.setAttribute("error", errorMsg);
            RequestDispatcher view=request.getRequestDispatcher("/add_dvd.view");
            view.forward(request, response);
            return;
            }
            
            
            
            
            
            //capta negocio
            
            DVDItem item = new DVDItem(title,year,genre);
//            
//            PrintWriter out= response.getWriter();
//            
//            out.println("SUCCESS: TITLE " +item.getTitle());
//            out.println("SUCCESS: YEAR " +item.getYear());                    
//            out.println("SUCCESS: GENRE " +item.getGenre());
//            out.close();
            
            request.setAttribute("clase", item);
            
            RequestDispatcher view= request.getRequestDispatcher("/success.view");
            view.forward(request, response);
            
            
            
        }
        
        catch (RuntimeException e) {
            
            errorMsg.add("ERROR: SUPER ERROR FATAL" +e.getMessage());
            request.setAttribute("error", errorMsg);
            RequestDispatcher view=request.getRequestDispatcher("/add_dvd.view");
            view.forward(request, response);
            
        }
        
        
        
        
        
        
        
        
        
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
